package zhtt;

import org.junit.Test;
import zhtt.JunitTestUtil;
import zhtt.demo01.Country;

import java.util.List;

/**
 * Created by zhtt on 2017/4/13.
 */
public class JunitTest01 extends JunitTestUtil {

    @Test
    public void get(){
        try{
            Country country=session.get(Country.class,3);
            System.out.println(country.getName());
            Country country2=session.get(Country.class,3);
            System.out.println(country2.getName());
            session.clear();
            Country country3=session.get(Country.class,3);
            System.out.println(country3.getName());
        }catch (Exception e){

        }
    }

    @Test
    public void evictEntityRegion(){
        Country country=session.get(Country.class,3);
        System.out.println(country.getName());
        Country country2=session.get(Country.class,3);
        System.out.println(country2.getName());
        session.clear();
        Country country3=session.get(Country.class,3);
        System.out.println(country3.getName());
        session.clear();
        HbnUtil.evictEntityRegion(Country.class);
        Country country4=session.get(Country.class,3);
        System.out.println(country4.getName());
    }

    @Test
    /** Query 缓存是不可重复读的，也就是对象被修改手，缓存消失
     * query查询缓存
     * 将执行三次查询语句，分别是在createQuery()执行时【可以与下面的测试方法对比，下面的只执行2次sql】
     */
    public void queryCache(){
        List<Country> countryList=session.createQuery("from Country").list();//第一次sql
        System.out.println("第一次Query查询："+countryList.size());
        Country country=session.get(Country.class,3);
        System.out.println(country.getName());
        session.clear();
        Country country1=session.get(Country.class,3);
        System.out.println(country1.getName());
        Country country2=(Country)session.createQuery("from Country where id=3").uniqueResult();//第二次sql
        System.out.println(country2.getName());
        Country country3=(Country)session.createQuery("from Country where id=3").uniqueResult();//第三次sql
        System.out.println(country3.getName());
    }

    @Test
    /**
     * query查询缓存
     * 开启 Query缓存 ：<property name="hibernate.cache.use_query_cache">true</property>
     * 【执行两次sql查询，上面的测试方法执行三次】
     */
    public void queryCache2(){
        List<Country> countryList=session.createQuery("from Country").setCacheable(true).list();//第一次sql
        System.out.println("第一次Query查询："+countryList.size());
        Country country=session.get(Country.class,3);
        System.out.println(country.getName());
        session.clear();
        Country country1=session.get(Country.class,3);
        System.out.println(country1.getName());
        Country country2=(Country)session.createQuery("from Country where id=3").setCacheable(true).uniqueResult();//第二次sql
        System.out.println(country2.getName());
        Country country3=(Country)session.createQuery("from Country where id=3").setCacheable(true).uniqueResult();
        System.out.println(country3.getName());
    }
}
